import React from 'react';

import QiniuUploadimage from '../QiniuUploadimage';
import {Icon} from 'antd';

import styles from './index.less';

export default class FileEx extends React.Component{

  state = {
    value:""
  }

  onFetchHash(hash){
    this.props.emitter.emit('onValueChange', this.props.name, hash);
    this.setState({
      value:hash
    })
  }

  iemi = null;

  reset(){
    this.setState({
      value:this.props.defaultValue?this.props.defaultValue:""
    })
  }

  componentWillReceiveProps(n){
    this.setState({
      value:n.defaultValue
    })
  }

  componentWillMount(){
    this.reset();

    this.iemi = this.props.emitter.addListener('onReset', ()=>{this.reset()});
    this.props.emitter.emit('onValueChange', this.props.name, this.props.defaultValue);
  }

  componentWillUnmount(){
    this.iemi.remove();
  }

  render(){
    return <div>
      <Icon type = "delete" onClick = {()=>{
        this.props.emitter.emit('onValueChange', this.props.name, "");
        this.setState({
          value:""
        })
      }}/> 
      <QiniuUploadimage name = {this.props.name} hash = {this.state.value} defaultHash = {this.props.defaultValue} onFetchHash = {(hash)=>{
        this.onFetchHash(hash);
      }}/>
    </div>
  }
}